home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / answers / solar9.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.8 KB  |  381 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* solar9.c - a simple solar system.  
  19.  *    Added a reshape handler to reset the viewport and viewing volume.
  20.  *    Add depth buffering and back face culling.
  21.  *    Add a viewing transformation and control it using mouse input.
  22.  *    Enable lighting and give the sun, earth and moon material properties
  23.  *
  24.  *    F1 key            - print help information
  25.  *    Left Mouse Button    - change incidence and azimuth angles
  26.  *    Middle Mouse Button    - change the twist angle based on
  27.  *                  horizontal mouse movement
  28.  *    Right Mouse Button    - zoom in and out based on vertical
  29.  *                  mouse movement
  30.  *    <R> Key            - reset viewpoint
  31.  *    SPACE Key        - toggle between solid/wireframe models
  32.  *    Escape Key        - exit program
  33.  */
  34. #include <GL/gl.h>
  35. #include <GL/glu.h>
  36. #include <GL/glut.h>
  37.  
  38. #include <stdio.h>
  39. #include <math.h>
  40.  
  41. /* Function Prototypes */
  42.  
  43. GLvoid initgfx( GLvoid );
  44. GLvoid keyboard( GLubyte, GLint, GLint );
  45. GLvoid specialkeys( GLint, GLint, GLint );
  46. GLvoid mouse( GLint, GLint, GLint, GLint );
  47. GLvoid motion( GLint, GLint );
  48. GLvoid reshape( GLsizei, GLsizei );
  49. GLvoid drawScene( GLvoid );
  50.  
  51. void checkError( char * );
  52. void printHelp( char * );
  53.  
  54. void resetView( GLvoid );
  55. void polarView( GLfloat, GLfloat, GLfloat, GLfloat);
  56.  
  57. /* Global Variables */
  58.  
  59. static char *progname; 
  60.  
  61. static GLboolean filledFlag = GL_TRUE;
  62.  
  63. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  64. static GLint        action;
  65.  
  66. static GLdouble        xStart = 0.0, yStart = 0.0;
  67.  
  68. static GLfloat        sunRadius = 0.7;
  69. static GLfloat        earthRadius = 0.4, earthOrbit = 3.5; 
  70. static GLfloat        moonRadius = 0.2, moonOrbit = 1.0;
  71. static GLfloat         near, far, distance, twistAngle, incAngle, azimAngle;
  72.  
  73. /* Global Definitions */
  74.  
  75. #define KEY_ESC    27    /* ascii value for the escape key */
  76.  
  77. void
  78. main( int argc, char *argv[] )
  79. {
  80.     GLsizei width, height;
  81.  
  82.     glutInit( &argc, argv );
  83.  
  84.     /* create a window that is 1/4 the size of the screen,
  85.      * and position it in the middle of the screen.
  86.      */
  87.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  88.     height = glutGet( GLUT_SCREEN_HEIGHT );
  89.     glutInitWindowPosition( width / 4, height / 4 );
  90.     glutInitWindowSize( width / 2, height / 2 );
  91.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
  92.     glutCreateWindow( argv[0] );
  93.  
  94.     initgfx();
  95.  
  96.     glutReshapeFunc( reshape );
  97.     glutKeyboardFunc( keyboard );
  98.     glutSpecialFunc( specialkeys );
  99.     glutMouseFunc( mouse );
  100.     glutMotionFunc( motion );
  101.     glutDisplayFunc( drawScene ); 
  102.  
  103.     progname = argv[0];
  104.  
  105.     printHelp( progname );
  106.  
  107.     glutMainLoop();
  108. }
  109.  
  110. void
  111. printHelp( char *progname )
  112. {
  113.     fprintf(stdout, 
  114.         "\n%s - model some objects\n\n"
  115.         "F1 key        - print help information\n"
  116.         "Left Mousebutton    - move eye position\n"
  117.         "Middle Mousebutton    - change twist angle\n"
  118.         "Right Mousebutton    - move up / down to zoom in / out\n"
  119.         "<R> Key        - reset viewpoint\n"
  120.         "SPACE key    - toggle between solid/wireframe mode\n"
  121.         "Escape Key    - exit the program\n\n",
  122.         progname);
  123. }
  124.  
  125. GLvoid
  126. initgfx( GLvoid )
  127. {
  128.     GLfloat maxOrbit;
  129.  
  130.     /* Set up properties for the light */
  131.     GLfloat    sunSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  132.  
  133.     /* set clear color to black */
  134.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  135.  
  136.     /* enable the depth buffer */
  137.     glEnable( GL_DEPTH_TEST );
  138.  
  139.     /* enable the face culling */
  140.     glEnable( GL_CULL_FACE );
  141.  
  142.     /* Maximum size of all the objects in your scene */
  143.     maxOrbit = earthOrbit + moonOrbit + moonRadius;
  144.  
  145.     /* Set up near and far so that ( far - near ) > maxObjectSize, */
  146.     /* and determine the viewing distance (adjust for zooming) */
  147.     near = 1.0;
  148.     far = near + 8*maxOrbit; 
  149.  
  150.     resetView();
  151.  
  152.     /* Set the light properties */
  153.     glLightfv( GL_LIGHT0, GL_SPECULAR, sunSpecular );
  154.  
  155.     /* Turn on the default light */
  156.     glEnable( GL_LIGHT0 );
  157.  
  158.     /* Enable fast material changes for diffuse material */
  159.     glColorMaterial( GL_FRONT, GL_DIFFUSE );
  160. }
  161.  
  162. GLvoid 
  163. reshape( GLsizei width, GLsizei height )
  164. {
  165.     GLdouble    aspect;
  166.  
  167.     glViewport( 0, 0, width, height );
  168.  
  169.     /* compute aspect ratio */
  170.     aspect = (GLdouble) width / (GLdouble) height;
  171.  
  172.     glMatrixMode( GL_PROJECTION );
  173.  
  174.     /* Reset world coordinates first ... */
  175.     glLoadIdentity();
  176.  
  177.     /* Reset the viewing volume based on the new aspect ratio */
  178.     gluPerspective( 45.0, aspect, near, far );
  179.  
  180.     glMatrixMode( GL_MODELVIEW );
  181. }
  182.  
  183. void 
  184. checkError( char *label )
  185. {
  186.     GLenum error;
  187.     while ( (error = glGetError()) != GL_NO_ERROR )
  188.         printf( "%s: %s\n", label, gluErrorString(error) );
  189. }
  190.  
  191. GLvoid 
  192. keyboard( GLubyte key, GLint x, GLint y )
  193. {
  194.     switch (key) {
  195.     case ' ':    /* toggle fill mode */
  196.         filledFlag = !filledFlag;
  197.         glutPostRedisplay();
  198.         break;
  199.     case 'R':
  200.         resetView();
  201.         glutPostRedisplay();
  202.         break;
  203.     case KEY_ESC:    /* Exit when the Escape key is pressed */
  204.         exit(0);
  205.     }
  206. }
  207.  
  208. GLvoid 
  209. specialkeys( GLint key, GLint x, GLint y )
  210. {
  211.     switch (key) {
  212.     case GLUT_KEY_F1:    /* Function key #1 */
  213.         /* print help information */
  214.         printHelp( progname );
  215.         break;
  216.     }
  217. }
  218.  
  219. GLvoid 
  220. mouse( GLint button, GLint state, GLint x, GLint y )
  221. {
  222.     if (state == GLUT_DOWN) {
  223.         switch (button) {
  224.         case GLUT_LEFT_BUTTON:
  225.             action = MOVE_EYE;
  226.             break;
  227.         case GLUT_MIDDLE_BUTTON:
  228.             action = TWIST_EYE;
  229.             break;
  230.         case GLUT_RIGHT_BUTTON:
  231.             action = ZOOM;
  232.             break;
  233.         }
  234.  
  235.         /* Update the saved mouse position */
  236.         xStart = x;
  237.         yStart = y;
  238.     } else {
  239.         action = MOVE_NONE;
  240.     }
  241.  
  242. }
  243.  
  244. GLvoid
  245. motion( GLint x, GLint y )
  246. {
  247.     switch (action) {
  248.     case MOVE_EYE:
  249.         /* Adjust the eye position based on the mouse position */
  250.         azimAngle += (GLdouble) (x - xStart);
  251.         incAngle -= (GLdouble) (y - yStart);
  252.         break;
  253.     case TWIST_EYE:
  254.         /* Adjust the eye twist based on the mouse position */
  255.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  256.         break;
  257.     case ZOOM:
  258.         /* Adjust the eye distance based on the mouse position */
  259.         distance -= (GLdouble) (y - yStart)/10.0;
  260.         break;
  261.     default:
  262.         printf("unknown action %d\n", action);
  263.     }
  264.     
  265.     /* Update the stored mouse position for later use */
  266.     xStart = x;
  267.     yStart = y;
  268.  
  269.     glutPostRedisplay();
  270. }
  271.  
  272. void
  273. resetView( GLvoid )
  274. {
  275.     distance = near + (far - near) / 2.0;
  276.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  277.     incAngle = 0.0;
  278.     azimAngle = 0.0;
  279. }
  280.  
  281. void
  282. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  283.             GLfloat twist)
  284. {
  285.     glTranslatef( 0.0, 0.0, -distance);
  286.     glRotatef( -twist, 0.0, 0.0, 1.0);
  287.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  288.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  289. }
  290.  
  291. GLvoid
  292. drawScene( GLvoid )
  293. {
  294.     static GLfloat    year = 45.0, day = -90.0;
  295.  
  296.     static GLfloat  yellow[] = { 1.0, 1.0, 0.0, 1.0 };
  297.     static GLfloat  blue[] = { 0.0, 0.0, 1.0, 1.0 };
  298.     static GLfloat  gray[] = { 0.4, 0.4, 0.4, 1.0 };
  299.  
  300.     static GLfloat  noEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  301.     static GLfloat  defaultDiffuse[] = { 0.8, 0.8, 0.8, 1.0 };
  302.  
  303.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  304.  
  305.     glPushMatrix();
  306.  
  307.         /* set up viewing transformation */
  308.         polarView( distance, azimAngle, incAngle, twistAngle );
  309.  
  310.         glEnable( GL_LIGHTING );
  311.         glEnable( GL_COLOR_MATERIAL );
  312.  
  313.         /* draw sun */
  314.  
  315.         /* Give the sun a yellow glow */
  316.         glMaterialfv( GL_FRONT, GL_EMISSION, yellow );
  317.  
  318.         glPushMatrix();
  319.             /* rotate on our own axis */
  320.             glRotatef( 90.0, 1.0, 0.0, 0.0 );
  321.             if (filledFlag)
  322.                 glutSolidSphere( sunRadius, 15, 15 );
  323.             else
  324.                 glutWireSphere( sunRadius, 15, 15 );
  325.         glPopMatrix();
  326.  
  327.         /* turn emmission off while drawing planets */
  328.         glMaterialfv( GL_FRONT, GL_EMISSION, noEmission );
  329.  
  330.         /* set material for the earth */
  331.         glColor4fv( blue );
  332.  
  333.         glPushMatrix();
  334.  
  335.             /* draw earth */
  336.  
  337.             /* rotate to the right time of year */
  338.             glRotatef( year, 0.0, 1.0, 0.0 );
  339.  
  340.             /* translate out to our orbit about the sun */
  341.             glTranslatef( earthOrbit, 0.0, 0.0 );
  342.             glPushMatrix();
  343.                 /* rotate on our own axis */
  344.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  345.                 if (filledFlag)
  346.                     glutSolidSphere( earthRadius, 15, 15 );
  347.                 else
  348.                     glutWireSphere( earthRadius, 15, 15 );
  349.             glPopMatrix();
  350.  
  351.             /* set material for the moon */
  352.             glColor4fv( gray );
  353.  
  354.             /* draw moon */
  355.             glPushMatrix();
  356.                 /* rotate to the right time of day */
  357.                 glRotatef( day, 0.0, 1.0, 0.0 );
  358.  
  359.                 /* translate out to our orbit about the earth */
  360.                 glTranslatef( moonOrbit, 0.0, 0.0 );
  361.  
  362.                 /* rotate on our axis */
  363.                 glRotatef( 90.0, 1.0, 0.0, 0.0 );
  364.                 if (filledFlag)
  365.                     glutSolidSphere( moonRadius, 15, 15 );
  366.                 else
  367.                     glutWireSphere( moonRadius, 15, 15 );
  368.             glPopMatrix();
  369.  
  370.         glPopMatrix();
  371.  
  372.         glDisable( GL_COLOR_MATERIAL );
  373.         glDisable( GL_LIGHTING );
  374.  
  375.     glPopMatrix();
  376.  
  377.     checkError( "drawScene" );
  378.  
  379.     glFlush();
  380. }
  381.